<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\Admin\model;

use think\Model;
use think\Db;
use app\admin\validate\Node as NodeCheck;

class NodeM extends Model {
	protected $table = "am_node";
	
	public function add(){
        $data = request()->param();
		$this->checkData('add',$data);
        return $this->allowField(true)->create($data);
    }
    public function edit(){
        $data = request()->param();
		$this->checkData('edit',$data);
        return $this->allowField(true)->save($data, ['id' => $data['id']]);
    }
    public function del(){
        $data = request()->param();
		$this->checkData('del',$data);
		$res = $this->where('parentId',$data['id'])->find();
		if($res){
		$res['code'] = 1;
		$res['msg'] = '当前节点包含子节点!';
		echo json_encode($res);
		exit();		
			}
        return $this->where('id',  $data['id'])->delete();
    }
	/*验证器,验证*/
	protected function checkData($act,$data){
		$check = new NodeCheck;
		if (!$check->scene($act)->check($data)) {
			//return json(['msg'=>$check->getError(),'code'=>1]);
			$res['msg'] = $check->getError();
			$res['code'] = 1;
			echo json_encode($res);
			exit();
		}
	}
}	